package cn.zhoutingrui.pethome.system.shop.controller;

import cn.zhoutingrui.pethome.base.constants.BaseConstants;
import cn.zhoutingrui.pethome.system.emp.domain.Employee;
import cn.zhoutingrui.pethome.system.emp.service.IEmployeeService;
import cn.zhoutingrui.pethome.system.logininfo.domain.Logininfo;
import cn.zhoutingrui.pethome.system.logininfo.service.ILogininfoService;
import cn.zhoutingrui.pethome.system.shop.domain.ShopAuditLog;
import cn.zhoutingrui.pethome.system.shop.service.IShopService;
import cn.zhoutingrui.pethome.system.shop.domain.Shop;
import cn.zhoutingrui.pethome.system.shop.query.ShopQuery;
import cn.zhoutingrui.pethome.utils.AjaxResultUtil;
import cn.zhoutingrui.pethome.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResultUtil addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return AjaxResultUtil.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResultUtil json(@RequestBody ShopQuery query)
    {
        try {
            List<Shop> list = shopService.findQuery(query);
            Long count = shopService.findByQueryCount(query);
            AjaxResultUtil ajaxResultUtil = AjaxResultUtil.success().setData(list).setTotal(count);
            return ajaxResultUtil;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.fail();
        }
    }
    @GetMapping("/active/{id}")
    @ApiOperation("用户账号激活")
    public String active(@PathVariable("id")Long id){
        //修改店铺状态
        Shop byId = shopService.findById(id);
        byId.setRegisterTime(new Date());
        byId.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        shopService.update(byId);;
        //修改管理员状态
        Employee emp = employeeService.findById(byId.getAdminId());
        emp.setState(BaseConstants.Employee.STATE_NORMAL);
        employeeService.update(emp);
        //修修改logininfo中的状态
        Long logininfoId = emp.getLogininfoId();
        Logininfo logininfo = logininfoService.findById(logininfoId);
        logininfo.setDisable(BaseConstants.User.STATE_ENABLE);
        logininfoService.update(logininfo);
        return "激活成功";
    }

    @GetMapping("/export")
    @ApiOperation("商家信息导出")
    public AjaxResultUtil ecport(HttpServletResponse response){
        List<Shop> all = shopService.findAll();
        ExcelUtil.exportExcel(all,"商家信息统计","统计表",Shop.class,"商家统计.xls",response);
        return AjaxResultUtil.success();
    }

    @PostMapping("/import")
    @ApiOperation("导入excel文件")
    public void importExcel(@RequestPart("file")MultipartFile file){
        List<Shop> list = ExcelUtil.importExcel(file,1,1,Shop.class);
        list.forEach(System.out::println);
    }

}
